<template>
  <div>
    <Row class="header" type="flex" justify="center">
      <Col span="12">
        <Icon class="icon" type="md-pie"/>
        <span class="title">接收总数</span>
      </Col>
      <Col span="10">
        <Select @on-change="change" v-model="model1" v-if="cityList.length>0" filterable style="width:100%">
          <Option v-for="(item,index) in cityList" :value="item.value" :key="index">{{ item.label }}</Option>
        </Select>
      </Col>
    </Row>
    <br>
    <div id="TypesPieTwo" :style="{ width: width + 'px', height: height + 'px'}"></div>
  </div>
</template>

<script>
import echarts from 'echarts'
// import { getPeopleTypeNum } from '@/api/statistics'
import { findCompanyList, getStatisticJSNum } from '@/api/record'
// import mockData from './mockData'

export default {
  name: 'TypesPieEchart',
  data () {
    return {
      chartData: [],
      cityList: [
      ],
      model1: '全部'
    }
  },
  props: {
    width: {
      type: Number,
      default: () => {
        return 500
      }
    },
    height: {
      type: Number,
      default: () => {
        return 300
      }
    }
  },
  mounted () {
    this.chart()
    this.init()
  },
  methods: {
    change () {
      console.log(this.model1)
      if (this.model1 === '全部') {
        this.chart()
      } else {
        this.chart(this.model1)
      }
    },
    init () {
      findCompanyList({ company_type: '2' }).then(ret => {
        if (ret.data.errcode === 0) {
          let dataList = ret.data.data
          let newArray = [{ label: '全部', value: '全部' }]
          for (let i = 0; i < dataList.length; i++) {
            if (dataList[i].code) {
              newArray.push({
                label: dataList[i].name,
                value: dataList[i].code
              })
            }
          }
          this.cityList = newArray
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    },
    chart (data) {
      getStatisticJSNum({ company_code: data }).then(ret => {
        if (ret.data.errcode === 0) {
          let JSnum = 0
          let SBnum = 0
          if (ret.data.data === '') {
            JSnum = 0
            SBnum = 0
          } else {
            JSnum = ret.data.data.JSnum
            SBnum = ret.data.data.SBnum
          }
          // 数据处理
          let arr = []
          arr.push({ name: '已经接收总数', num: JSnum })
          arr.push({ name: '上报总数', num: SBnum })
          this.chartData = arr
          let option = {
            title: {
              text: ''
            },
            textStyle: {
              // color: '#bababa'
            },
            legend: {
              orient: 'vertical',
              left: 20,
              top: 20,
              data: ['已经接收总数', '上报总数'],
              formatter: function (name) {
                var index = 0
                var clientlabels = ['已经接收总数', '上报总数']
                var clientcounts = [JSnum, SBnum]
                clientlabels.forEach(function (value, i) {
                  if (value === name) {
                    index = i
                  }
                })
                return name + ':' + clientcounts[index]
              }
            },
            tooltip: {
              trigger: 'item',
              backgroundColor: 'rgba(50,50,50,0.6)',
              // formatter: '{a} <br/>{b} : {c} ({d}%)'
              formatter: function (params, ticket, callback) {
                return params.value.name + ': ' + params.value.num + '人  (' + params.percent + '%)'
              }
            },
            dataset: {
              source: []
            },
            series: [
              {
                type: 'pie',
                // radius: '55%',
                radius: ['30%', '50%'],
                center: ['60%', '60%'],
                color: ['#ff9f7f', 'rgba(255,167,41,0.35)'],
                label: {
                  normal: {
                    show: false,
                    position: 'center'
                  },
                  emphasis: {
                    show: true,
                    textStyle: {
                      fontSize: '25',
                      fontWeight: 'bold'
                    }
                  }
                },
                itemStyle: {
                  // 通常情况下：
                  normal: {
                  },
                  // 鼠标悬停时：
                  emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                  },
                  encode: {
                    itemName: 'name',
                    value: 'num'
                  }
                }
              }
            ]
          }
          this.chartData.forEach(item => {
            option.dataset.source.push(item)
          })
          let myChart = echarts.init(document.getElementById('TypesPieTwo'))
          myChart.setOption(option)
        } else {
          this.$Message.error({
            content: '数据获取失败,\n' + ret.data.errmsg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  .header {
    height: 30px;
  }

  .icon {
    font-size: 28px;
    font-weight: 600;
    margin-right: 5px;
  }

  .title {
    font-size: 16px;
    font-weight: 600;
    line-height: 30px;
    color: #333333;
  }
</style>
